<?php

namespace App\Http\Controllers;

use App\Models\Qnr;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Http\Request;
use Illuminate\Support\Env;

class QnrController extends Controller
{

    /**
     * Display a listing of the resource.
     *
     * @param $classify
     * @param $tags
     *
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View|\Illuminate\Foundation\Application
     */
    public function index($classify = null,$tags = null)
    {

        $result['result'] = Qnr::where('is_using',Env::get('APP_CONTENT_STATE'))
            ->when($classify,function(Builder $query,string $classify) {
                if(!empty($classify)) {
                    $query->where('c_key',$classify);
                }
            })
            ->when($tags,function(Builder $query,string $tags) {
                if(!empty($tags)) {
                    $query->where('t_key',$tags);
                }
            })
            ->paginate(getConf('POST_VIEW_NUM'));

        return view(config('template') . '.qnr.index',['result' => $result]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param \Illuminate\Http\Request $request
     *
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param $slug
     *
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View|\Illuminate\Foundation\Application
     */
    public function show($slug)
    {

        $result['result'] = Qnr::where('slug',$slug)
            ->where('is_using',Env::get('APP_CONTENT_STATE'))
            ->with('qnr_cls')
            ->firstOrFail();

        if(empty($result['result'])) {
            abort(404);
        }

        return view(config('template') . '.qnr.show',['result' => $result]);
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param int $id
     *
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param \Illuminate\Http\Request $request
     * @param int                      $id
     *
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request,$id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param int $id
     *
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}
